<template>
  <div class="main-container">
    <div class="main-header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>品牌案例内容管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="wrap">
      <div class="content">
        <div class="search-container">
          <el-form class="search-form" :model="form" ref="form" label-width="90px" label-position="right" inline>
            <el-row>
              <el-form-item label="标题" prop="courseTitle">
                <el-input v-model="form.courseTitle"></el-input>
              </el-form-item>
              <el-form-item label="类型" prop="contentType">
                <el-select v-model="form.contentType">
                  <el-option :value="1" label="图文课程"></el-option>
                  <el-option :value="2" label="视频课程"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="状态" prop="status">
                <el-select v-model="form.status">
                  <el-option :value="1" label="草稿"></el-option>
                  <el-option :value="2" label="已发布"></el-option>
                  <el-option :value="3" label="下线"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="分类" prop="courseCode" v-if="showTotal">
                <el-select v-model="form.courseCode">
                 <el-option v-for="item in courseTypeList" :value="item.courseCode" :label="item.courseName" :key="item.courseCode"></el-option>
                </el-select>
              </el-form-item>
              
            </el-row>
            <el-row style="margin-left:40px">
              <el-form-item>
                <el-button type="primary" @click="onSearch">查询</el-button>
                <el-button @click="onReset">重置</el-button>
                <el-button @click="showTotal=!showTotal" type="text">{{showTotal ? '收起' : '展开'}}<i class="el-icon--right" :class="{'el-icon-arrow-down':!showTotal,'el-icon-arrow-up':showTotal}"></i></el-button>
              </el-form-item>
            </el-row>
          </el-form>
        </div>
        <div class="mt-20">
          <el-button type="primary" @click="onAdd"><i class="el-icon-plus"></i>新增品牌案例</el-button>
          <!--
          <el-button type="danger" @click="onRemove" disabled><i class="el-icon-delete"></i> 批量删除</el-button>
          -->
        </div>
        <div class="mt-10">
          <el-table :data="data" v-loading="loading">
            <el-table-column prop="courseTitle" label="课程标题" fixed min-width="150" show-overflow-tooltip>
              <template slot-scope="scope">
                <span>{{scope.row.courseTitle}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="contentType" label="课程类型" min-width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.contentType == 1">图文课程</span>
                <span v-if="scope.row.contentType == 2">视频课程</span>
              </template>
            </el-table-column>
            <el-table-column label="课程类目" min-width="250">
              <template slot-scope="scope">
                <span>{{scope.row.oneType}} > {{scope.row.twoType}}</span>
              </template>
            </el-table-column>  
            <el-table-column prop="readTimes" label="阅读次数" min-width="100" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="sort" label="排序" min-width="80">
              <template slot-scope="scope">
                <span>{{(scope.row.sort != 0 && (scope.row.sort == '' || scope.row.sort == null)) ? '--' : scope.row.sort}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" min-width="100">
              <template slot-scope="scope">
                <span v-if="scope.row.status==1">草稿</span>
                <span v-if="scope.row.status==2">已发布</span>
                <span v-if="scope.row.status==3">下线</span>
              </template>
            </el-table-column>
            <el-table-column label="操作"  width="240" fixed="right">
              <template slot-scope="scope">
                <el-button type="text" v-if="scope.row.status != 2" @click="changeStatus(scope.row.courseInfoId, 2)">上线</el-button>
                <el-button type="text" v-if="scope.row.status == 2" @click="changeStatus(scope.row.courseInfoId, 3)">下线</el-button>
                <el-button type="text" @click="goDetail(scope.row.courseInfoId)">查看</el-button>
                <el-button type="text" @click="doEdit(scope.row.courseInfoId)">修改</el-button>
                <el-button type="dangertext" @click="doDelete(scope.row.courseInfoId)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="mt-10 t-right" v-if="total > 0">
          <el-pagination
            background
            :current-page="currentPage"
            :total="total"
            :page-sizes="[10, 20, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        currentPage: 1,
        pageSize: 10,
        form: {},
        data: [],
        showTotal: false,
        total: 0,
        loading: false,
        courseTypeList: []
      }
    },
    activated() {
      this.loadData()
    },
    created() {
      this.getCourseTypeList()
      this.loadData()
    },
    methods: {
      getCourseTypeList(){
        const params = {
          documentType: 2,
          courseLevel: 2,
        }
        this.$api.courseType.getList(params).then(res => {
          if(res.success){
            this.courseTypeList = res.data
          }
        })
      },
      loadData() {
        this.loading = true
        const params ={
          ...this.form,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          documentType: 2,
          sortType: 0
        }
        this.$api.course.getList(params).then(res => {
          if(res.success){
            this.data = res.data.list
            this.total = res.data.total
            this.loading = false
          }else {
            this.loading = false
          }
        }).catch(err=> {
          this.loading = false
        })
      },
      onSearch() {
        this.currentPage = 1
        this.loadData()
      },
      onReset() {
        this.$refs.form.resetFields()
        this.currentPage = 1
        this.loadData()
      },
      // 新增
      onAdd(){
        this.$router.push('/partner/news/add')
      },
      // 批量删除
      onRemove() {

      },
      doEdit(id) {
        this.$router.push('/partner/news/edit/' + id)
      },
      goDetail(id) {
        this.$router.push('/partner/news/detail/' + id)
      },
      doDelete(id) {
        this.$confirm('删除后不可还原，确认删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.$api.course.delete(id).then(res => {
            if(res.success) {
              this.$message.success('已删除')
              this.currentPage = 1
              this.loadData()
            }
          })
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.currentPage = 1
        this.loadData()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.loadData()
      },
      changeStatus(id, type){
        this.$api.course.onOffLine({courseInfoId: id, status: type}).then(res => {
          if(res.success) {
            this.$message.success('操作成功')
            this.loadData()
          }
        })
      }
    }
  }
</script>
